<?php
namespace app\common\component\user;

use think\Session;

class UserService {
    public function login($data) {
    	$userModel = UserModel::get(['username' => $data['username']]);

		if($userModel) {
			// 用户状态请查看数据表备注
			switch($userModel -> getData('status')) {
	    		case 1:
	    			return [
	    				'code' => -1,
	    				'message' => '账号待激活'
	    			];
	    		case 2:
	    			return [
	    				'code' => -1,
	    				'message' => '账号已注销'
	    			];
	    		case 3:
	    			return [
	    				'code' => -1,
	    				'message' => '账号已禁止'
	    			];
	    		case 4:
	    			return [
	    				'code' => -1,
	    				'message' => '账号已冻结'
	    			];
	    		default:
	    			if(checkPasswordHash($data['password'], $userModel -> password)) {
						$this -> setLoginSession($userModel -> user_id, $userModel -> username);

	    				return [
	    					'code' => 0,
	    					'message' => '登陆成功!'
	    				];
	    			}
	    	}
		}
    	return [
			'code' => -1,
			'message' => '账号或密码错误!'
		];
    }

    public function setLoginSession($user_id, $username) {
    	$this -> clearLoginSession();

		Session::set('user_id', $user_id);
		Session::set('user_name', $username);

		$resultUserVip = (new \app\common\component\user_vip\UserVipService) -> clearVip($user_id);
		Session::set('is_vip', !!$resultUserVip['data']['user_vip'] -> days);
    }

    public function clearLoginSession() {
    	// 清空相关session
		Session::delete('user_id');
		Session::delete('user_name');
		Session::delete('is_vip');
    }

    public function isLogin() {
    	if(Session::has('user_id') && Session::has('user_id') > 0) {
    		return [
    			'code' => 0,
    			'message' => '已登陆'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '未登陆'
		];
    }

	public function checkPayPassword($pay_password) {
		$getUser = $this -> getUser();

		if(checkPasswordHash($pay_password, $getUser['data']['user'] -> pay_password)) {
			return [
				'code' => 0,
				'message' => '安全密码正确!',
				'data' => [
					'user' => $getUser
				]
			];
		}

		return [
			'code' => -1,
			'message' => '安全密码错误!'
		];
	}
	
    public function getUser($user_id = null) {
    	if($user_id && $user_id > 0) {
    		$getUser = UserModel::get(['user_id' => $user_id]);
    	} else if(Session::has('user_id')) {
    		$getUser = UserModel::get(['user_id' => Session::get('user_id')]);
    	} else {
    		$getUser = false;
    	}

		if($getUser) {
			return [
				'code' => 0,
				'data' => [
					'user' => $getUser
				]
			];
		}

		return [
			'code' => -1,
			'message' => '该用户不存在'
		];
    }

    public function getUsers($page = 10) {
		return (new UserModel()) -> order('status DESC, user_id DESC') -> paginate($page);
	}

    public function create($data) {
    	if(!empty($data['promotion_username'])) {
    		$getPromotionUser = UserModel::get(['username' => $data['promotion_username']]);
    		if($getPromotionUser) {
    			$data['promotion_id'] = $getPromotionUser['user_id'];
    		} else {
    			$data['promotion_id'] = 0;
    		}
    	}

    	unset($data['captcha']);
    	unset($data['comfirm_password']);
    	unset($data['comfirm_pay_password']);
    	unset($data['promotion_username']);

    	$data['password'] = getPasswordHash($data['password']);
    	$data['pay_password'] = getPasswordHash($data['pay_password']);

    	$userModel = new UserModel($data);
    	$userModel -> status = 0;
    	$userModel -> createtime = time();

		// $userModel -> allowField(true) -> save();
    	if($userModel -> save()) {
    		return [
    			'code' => 0,
    			'message' => '注册成功',
    			'data' => [
    				'user' => $userModel
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '注册失败'
		];
    }

    public function update($data, $user_id = null) {
    	$userModel = new UserModel();

    	if($user_id && $user_id > 0) {
    		$updateUser = $userModel -> where('user_id', $user_id) -> update($data);
    	} else if(Session::has('user_id')) {
    		$updateUser = $userModel -> where('user_id', Session::get('user_id')) -> update($data);
    	} else {
    		$updateUser = false;
    	}

    	if($updateUser) {
    		return [
    			'code' => 0,
    			'message' => '修改成功'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '修改失败'
		];
    }

    public function delete($user_id) {
		$getUser = UserModel::get($user_id);

    	if($getUser && $getUser -> delete()) {
    		$getUserSign = \app\common\component\user_sign\UserSignModel::get($user_id);
    		($getUserSign && $getUserSign -> delete());

    		$getUserVip = \app\common\component\user_vip\UserVipModel::get($user_id);
    		($getUserVip && $getUserVip -> delete());

    		$getUserWallet = \app\common\component\user_wallet\UserWalletModel::get($user_id);
    		($getUserWallet && $getUserWallet -> delete());

    		$getUserWalletFreeze = \app\common\component\user_wallet_freeze\UserWalletFreezeModel::get($user_id);
    		($getUserWalletFreeze && $getUserWalletFreeze -> delete());

    		$getUserWalletLogs = \app\common\component\user_wallet_log\UserWalletLogModel::where('to_user_name', $getUser -> username) -> delete();

    		return [
    			'code' => 0,
    			'message' => '用户删除成功'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '用户删除失败'
		];
	}
}
?>